/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-10-13 10:59:32
 * @LastEditTime: 2025-10-14 14:35:52
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\lc\handheld\sl_app.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include <math.h>
#include "handheld.h"
#include "qmi8658.h"
#include "bsp_lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"

extern lv_obj_t *icon_in_obj; // 应用界面
extern int icon_flag;

extern i2c_obj_t i2c0_master;

static lv_obj_t * label_x; // x角度值
static lv_obj_t * label_y; // y角度值
static lv_obj_t * led_att; // 实心圆点

static lv_timer_t * my_lv_timer;

/**
 * @description: 返回主界面向上滑动事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void my_gesture_event_cb(lv_event_t *e)
{
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    if (dir == LV_DIR_TOP)
    {
        lv_obj_del(icon_in_obj); //删除画布
        lv_timer_del(my_lv_timer);
        qmi8658_close(); // 关闭芯片运行
        icon_flag = 0;
    }
}

/**
 * @description:  定时更新姿态角度值
 * @param {lv_timer_t *} timer
 * @return {*}
 */
static void att_update_cb(lv_timer_t * timer)
{
    qmi8658_t qmi8658;
    int att_x, att_y, att_z;
    // 获取XYZ角度
    qmi8658_fetch_angleFromAcc(&qmi8658);
    att_x = round(qmi8658.AngleX);
    att_y = round(qmi8658.AngleY);
    att_z = round(qmi8658.AngleZ);

    // 更新角度值
    lv_label_set_text_fmt(label_x, "X=%d°", -att_y);
    lv_label_set_text_fmt(label_y, "Y=%d°", -att_x);

    lv_obj_align(led_att, LV_ALIGN_CENTER, -att_y, -att_x);
}

/**
 * @description: 水平仪应用
 * @return {*}
 */
void sl_app(void)
{
    qmi8658_init(i2c0_master);
     
    lvgl_port_lock(0);

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 10);
    lv_style_set_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_color_hex(0x00BFFF));
    lv_style_set_bg_grad_color(&style, lv_color_hex(0x00BF00));
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);
    lv_style_set_border_width(&style, 0);
    lv_style_set_pad_all(&style, 0);
    lv_style_set_width(&style, 320);
    lv_style_set_height(&style, 240);
    
    // 创建一个界面
    icon_in_obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(icon_in_obj, &style, 0);

    // 创建x角度标签
    label_x = lv_label_create(icon_in_obj);
    lv_label_set_text(label_x, "");
    lv_obj_set_style_text_color(label_x, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_x, &lv_font_montserrat_20, 0);
    lv_obj_align(label_x, LV_ALIGN_TOP_LEFT, 20, 20);

    // 创建y角度标签
    label_y = lv_label_create(icon_in_obj);
    lv_label_set_text(label_y, "");
    lv_obj_set_style_text_color(label_y, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_y, &lv_font_montserrat_20, 0);
    lv_obj_align(label_y, LV_ALIGN_TOP_RIGHT, -20, 20);

    // 创建圆弧
    lv_obj_t *arc = lv_arc_create(icon_in_obj);
    lv_obj_set_size(arc, 200, 200);
    lv_obj_center(arc);
    lv_obj_remove_style(arc, NULL, LV_PART_KNOB); /* 移除旋钮 */
    lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE); /* 清除可点击属性 */
    lv_arc_set_bg_angles(arc, 0, 360); /* 设置背景弧的起始角度和终止角度 */
    lv_arc_set_value(arc, 360);        /* 圆弧当前值 */

    // 创建实心圆点
    led_att = lv_led_create(arc);
    lv_obj_center(led_att);
    lv_led_set_color(led_att, lv_palette_main(LV_PALETTE_RED));
    lv_led_set_brightness(led_att, 255);

    // 返回提示符
    lv_obj_t *label_back = lv_label_create(icon_in_obj);
    lv_label_set_text(label_back, LV_SYMBOL_UP);
    lv_obj_set_style_text_color(label_back, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(label_back, &lv_font_montserrat_20, 0);
    lv_obj_align(label_back, LV_ALIGN_BOTTOM_RIGHT, -20, -20);

    // 添加向上滑动退出功能
    lv_obj_add_event_cb(icon_in_obj, my_gesture_event_cb, LV_EVENT_GESTURE, 0);
    lv_obj_clear_flag(icon_in_obj, LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_flag(icon_in_obj, LV_OBJ_FLAG_CLICKABLE);

    lvgl_port_unlock();

    icon_flag = 10;

    // 创建一个lv_timer 用于更新角度
    my_lv_timer = lv_timer_create(att_update_cb, 100, NULL);
}